package com.lzy.cloud.base.facade;

import com.lzy.cloud.base.constants.AreaParam;
import com.lzy.cloud.base.dto.BaseAreaDto;
import com.lzy.cloud.base.dto.BaseAreaRecordDto;
import com.lzy.cloud.base.service.AreaRecordService;
import com.lzy.cloud.base.service.AreaService;
import com.lzy.cloud.base.util.AreaUtil;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
@Slf4j
public class AreaFacade {

    @Autowired
    private AreaService areaService;

    @Autowired
    private AreaRecordService areaRecordService;

    public Boolean addArea(BaseAreaDto baseAreaDto){
        //获取大写
        baseAreaDto.setEnName(AreaUtil.getPinYinHeadChar(baseAreaDto.getName()));
        //经纬度
        String result = AreaUtil.sendGet(AreaParam.BAIDU_API_URL,AreaParam.BAIDU_API_PARAM+baseAreaDto.getName());
        JSONObject resultMap = JSONObject.fromObject(result);
        if(null != resultMap.get("result") && !"".equals(resultMap.get("result")) && !"[]".equals(resultMap.get("result").toString())){
            JSONObject AreaResult = JSONObject.fromObject(resultMap.get("result"));
            JSONObject location = JSONObject.fromObject(AreaResult.get("location"));
            String lng = location.getString("lng");
            String lat = location.getString("lat");
            baseAreaDto.setLatitude(new BigDecimal(lat));
            baseAreaDto.setLongitude(new BigDecimal(lng));
        }
        if(baseAreaDto.getCityCode().toString().substring(2).equals("0000")){
            baseAreaDto.setLevel(1);
        }else if(baseAreaDto.getCityCode().toString().substring(4).equals("00")){
            baseAreaDto.setLevel(2);
        }else{
            baseAreaDto.setLevel(3);
        }
        Long areaId = areaService.insert(baseAreaDto);
        Ensure.that(areaId > 0 ).isTrue("00020007");
        //新增记录
        BaseAreaRecordDto baseAreaRecordDto = DozerUtils.map(baseAreaDto,BaseAreaRecordDto.class);
        baseAreaRecordDto.setAreaId(areaId);
        Long areaRecordId = areaRecordService.insert(baseAreaRecordDto);
        Ensure.that(areaRecordId > 0 ).isTrue("00020007");
        return true;
    }

    public Boolean updateArea(BaseAreaDto baseAreaDto){
        //获取大写
        baseAreaDto.setEnName(AreaUtil.getPinYinHeadChar(baseAreaDto.getName()));
        //经纬度
        String result = AreaUtil.sendGet(AreaParam.BAIDU_API_URL,AreaParam.BAIDU_API_PARAM+baseAreaDto.getName());
        JSONObject resultMap = JSONObject.fromObject(result);
        if(null != resultMap.get("result") && !"".equals(resultMap.get("result")) && !"[]".equals(resultMap.get("result").toString())){
            JSONObject AreaResult = JSONObject.fromObject(resultMap.get("result"));
            JSONObject location = JSONObject.fromObject(AreaResult.get("location"));
            String lng = location.getString("lng");
            String lat = location.getString("lat");
            baseAreaDto.setLatitude(new BigDecimal(lat));
            baseAreaDto.setLongitude(new BigDecimal(lng));
        }
        if(baseAreaDto.getCityCode().toString().substring(2).equals("0000")){
            baseAreaDto.setLevel(1);
        }else if(baseAreaDto.getCityCode().toString().substring(4).equals("00")){
            baseAreaDto.setLevel(2);
        }else{
            baseAreaDto.setLevel(3);
        }
        areaService.update(baseAreaDto);
        //新增记录
        BaseAreaRecordDto baseAreaRecordDto = new BaseAreaRecordDto();
        baseAreaRecordDto.setAreaYear(baseAreaDto.getAreaYear());
        baseAreaRecordDto.setCityCode(baseAreaDto.getCityCode());
        baseAreaRecordDto.setName(baseAreaDto.getName());
        baseAreaRecordDto.setAreaId(baseAreaDto.getId());
        Long areaRecordId = areaRecordService.insert(baseAreaRecordDto);
        Ensure.that(areaRecordId > 0 ).isTrue("00020009");
        return true;
    }
}
